Skip to content

fix: classify a subagent from its spawn event so the held prompt waits for it - #461

Merged
william0wang merged 1 commit into
mainfrom
fix/subagent-task-kind
Dec 1, 2026
Merged

william0wang merged 1 commit into
mainfrom
fix/subagent-task-kind

Conversation

@william0wang

Copy link
Copy Markdown
Owner

Summary

On 4.0, the first event a subagent produces is its spawn — SubagentSpawned, a generic session.updated carrying childSessionId and no taskKind (subagent/runner.ts:353-369; the child is ready before the Agent call returns). The kind only rides the task event that follows. BackgroundTaskListener froze taskKind at first sight, so every real subagent task stayed kindless for good, and every taskKind !== "subagent" check read "not a subagent":

  • the held-prompt chain's subagentWorkOutstanding() → session/prompt returned while the agent was still running;
  • the heartbeat's outstandingSubagentCallIds() → no card refresh;
  • markOutstandingSubagentsTerminal() → the safety net never fired.

The V2 face already classifies by childSessionId for a kindless payload (kindOf, #459). This brings the V1 listener to the same rule and, additionally, re-classifies a tracked task when a later sighting names its kind.

Change

src/handlers/background-tasks.ts

  • isSubagentTask(task): taskKind === "subagent", or no taskKind and a childSessionId. A present taskKind always wins.
  • TrackedTask.childSessionId stored at all three mint sites.
  • onTaskStatus backfills taskKind / childSessionId on every sighting, before any early return.
  • The three predicates read isSubagentTask.

Verification

  • tests/background-tasks.test.ts: 7 cases on the real 4.0 order (spawn → task event), kind-first, kindless-without-child, explicit non-subagent kind with a child id, late-named kind, safety net. Reverse-checked: dropping the childSessionId fallback fails 2, dropping the backfill fails 1.
  • Full suite: 155 files / 1744 tests, typecheck and lint clean.
  • Live against 4.0.0, V1 client, GLM-5.3-Flash, two background Explore agents — before: session/prompt resolved at t+9.3s with the last agent card completing at t+15.1s; after: prompt at t+11.0s / t+14.3s, cards done at t+7.6s / t+10.5s (prompt waits for the cards, two runs). Frames: .zcode/scratch/v2-probes/live/out/frames-held.jsonl.

Found while reviewing #453, whose _zcode/pendingBackgroundWork and result-text capture both key on taskKind === "subagent" and therefore read empty on a real backend; that PR should rebase onto this.

…s for it

On 4.0 the first event for a subagent is its spawn (SubagentSpawned, a
generic session.updated carrying childSessionId and no taskKind); the kind
only rides the task event that follows. The listener froze the kind at
first sight, so every real subagent task stayed kindless for good and the
held-prompt chain read "nothing outstanding": session/prompt returned while
the agent was still running (live: prompt at t+9.3s, the card completed at
t+15s). The V2 face already classified this way (kindOf, #459); the V1
listener now does the same, and a later sighting that names the kind
re-classifies the same tracked task.
@william0wang
william0wang merged commit 715a309 into main Dec 1, 2026
2 checks passed
@william0wang
william0wang deleted the fix/subagent-task-kind branch December 1, 2026 05:54
Dopetaiga added a commit to Dopetaiga/zcode-acp that referenced this pull request Dec 1, 2026
…ng#461); resultText from the output file instead of the notification turn

Second review round (2026-12-01):

- Rebased onto william0wang#461 (fix/subagent-task-kind): on 4.0 a subagent's first
  event is SubagentSpawned (childSessionId, NO taskKind), so every
  taskKind === "subagent" predicate stayed false on a real backend —
  pendingBackgroundWork answered subagentsOutstanding:false for a RUNNING
  agent and no card could ever carry a result. My liveness composition
  now rides william0wang#461's isSubagentTask (a present taskKind wins, else a
  childSessionId means subagent) over v2TaskOutcome(rawStatus).
- The result-text capture machinery is DROPPED per review: the
  notification turn's text is the PARENT model's commentary about the
  result, not the result (the agent's output lands in the file the
  terminal event names). A terminal subagent card now reads that file
  (best-effort, 64 KiB tail, surrogate-safe 4096-char clamp) into
  _meta.backgroundTask.resultText — no notification turn, no queue, no
  seal, no attribution guess; bash keeps its outputTail path.
- The stall-guard and subagentResultExpected nits are moot with the
  capture gone.
- Tests: spawn-first classification end-to-end (mint → kind backfill →
  terminal → file resultText), outputPath file resultText (+ missing
  file degrades cleanly), spawn-shape outstanding probe; capture tests
  removed with the machinery.

Gates: typecheck clean, lint 0 errors, full suite at parity with the
base failure set (pre-existing Windows-env-dependent files).
Dopetaiga added a commit to Dopetaiga/zcode-acp that referenced this pull request Dec 1, 2026
…ng#461); resultText from the output file instead of the notification turn

Second review round (2026-12-01):

- Rebased onto william0wang#461 (fix/subagent-task-kind): on 4.0 a subagent's first
  event is SubagentSpawned (childSessionId, NO taskKind), so every
  taskKind === "subagent" predicate stayed false on a real backend —
  pendingBackgroundWork answered subagentsOutstanding:false for a RUNNING
  agent and no card could ever carry a result. My liveness composition
  now rides william0wang#461's isSubagentTask (a present taskKind wins, else a
  childSessionId means subagent) over v2TaskOutcome(rawStatus).
- The result-text capture machinery is DROPPED per review: the
  notification turn's text is the PARENT model's commentary about the
  result, not the result (the agent's output lands in the file the
  terminal event names). A terminal subagent card now reads that file
  (best-effort, 64 KiB tail, surrogate-safe 4096-char clamp) into
  _meta.backgroundTask.resultText — no notification turn, no queue, no
  seal, no attribution guess; bash keeps its outputTail path.
- The stall-guard and subagentResultExpected nits are moot with the
  capture gone.
- Tests: spawn-first classification end-to-end (mint → kind backfill →
  terminal → file resultText), outputPath file resultText (+ missing
  file degrades cleanly), spawn-shape outstanding probe; capture tests
  removed with the machinery.

Gates: typecheck clean, lint 0 errors, full suite at parity with the
base failure set (pre-existing Windows-env-dependent files).
william0wang pushed a commit that referenced this pull request Dec 1, 2026
…ignal (#453)

* fix: classify subagents from their spawn event (rebased on #461); resultText from the output file instead of the notification turn

Second review round (2026-12-01):

- Rebased onto #461 (fix/subagent-task-kind): on 4.0 a subagent's first
  event is SubagentSpawned (childSessionId, NO taskKind), so every
  taskKind === "subagent" predicate stayed false on a real backend —
  pendingBackgroundWork answered subagentsOutstanding:false for a RUNNING
  agent and no card could ever carry a result. My liveness composition
  now rides #461's isSubagentTask (a present taskKind wins, else a
  childSessionId means subagent) over v2TaskOutcome(rawStatus).
- The result-text capture machinery is DROPPED per review: the
  notification turn's text is the PARENT model's commentary about the
  result, not the result (the agent's output lands in the file the
  terminal event names). A terminal subagent card now reads that file
  (best-effort, 64 KiB tail, surrogate-safe 4096-char clamp) into
  _meta.backgroundTask.resultText — no notification turn, no queue, no
  seal, no attribution guess; bash keeps its outputTail path.
- The stall-guard and subagentResultExpected nits are moot with the
  capture gone.
- Tests: spawn-first classification end-to-end (mint → kind backfill →
  terminal → file resultText), outputPath file resultText (+ missing
  file degrades cleanly), spawn-shape outstanding probe; capture tests
  removed with the machinery.

Gates: typecheck clean, lint 0 errors, full suite at parity with the
base failure set (pre-existing Windows-env-dependent files).

* fix: read the subagent result file from offset 0 (16 KiB bound)

Review approval follow-up (non-blocking, done now): output.txt holds only
the run's final message (writeCompletedAgentArtifacts joins the text
blocks and overwrites on a resumed run), so a 16 KiB head-read from
offset 0 covers any 4096-char resultText and removes the two artifacts
the 64 KiB tail read could produce — a mid-file stretch when the file
outgrew the window, and a possible U+FFFD seam when the tail start landed
inside a multi-byte character. Also documents that a CANCELLED-family
task's file carries the stopped-state message: hosts key "finished" off
the status, never off the presence of a resultText.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant